package com.example.property

import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.example.common.mvi.BaseMviActivity
import com.example.engin.GlideEngine
import com.example.path.RouterPath
import com.example.property.adapter.CarColorAdapter
import com.example.property.adapter.CarImageAdapter
import com.example.property.databinding.ActivityParkselectedBinding
import com.example.property.entity.CarApplyValues
import com.example.property.entity.CarColorValues
import com.example.property.entity.CarImageEntity
import com.example.property.intent.ParkSelectIntent
import com.example.property.uistate.ParkSelectUiState
import com.example.property.viewmodel.ParkSelectViewModel
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.sql.Date
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = RouterPath.PATH_SELECT_PARK)
class ParkSelectedActivity : BaseMviActivity<ActivityParkselectedBinding>() {


    private val mViewModel :ParkSelectViewModel by viewModels()

    @Autowired
    lateinit var carApplyValues :CarApplyValues

    private lateinit var mColorAdapter: CarColorAdapter
    private lateinit var mImageAdapter: CarImageAdapter


    override fun getLayoutId(): Int=R.layout.activity_parkselected
    override fun initViews() {

        ARouter.getInstance().inject(this)

        //模拟数据
        var startDate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(carApplyValues.parkStart.toLong() * 1000L)).toString()
        var endDate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(carApplyValues.parkEnd.toLong() * 1000L)).toString()
        mDataBinding.tvUseData.text="$startDate - $endDate"
        mDataBinding.tvApplyName.text=carApplyValues.parkName
        mDataBinding.tvApplyData.text=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(carApplyValues.parkTime.toLong() * 1000L)).toString()

        //车辆图片列表
        mDataBinding.applyparkingImgRv.layoutManager =  LinearLayoutManager(this@ParkSelectedActivity, LinearLayoutManager.HORIZONTAL, false)
        mImageAdapter = CarImageAdapter(R.layout.item_color_image)
        mDataBinding.applyparkingImgRv.adapter = mImageAdapter

        //添加车载图片
        mDataBinding.ivAddAction.setOnClickListener {
            PictureSelector.create(this@ParkSelectedActivity).openGallery(SelectMimeType.ofImage()).setImageEngine(
                GlideEngine.createGlideEngine()).forResult(object :
                OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>) {
                    var list = mutableListOf<CarImageEntity>()
                    result.forEach {
                        list.add(CarImageEntity(it.availablePath, it.availablePath.substring(it.availablePath.lastIndexOf("/"))))
                    }
                    mImageAdapter.data.addAll(list)
                    mImageAdapter.notifyDataSetChanged()

                    uploadImages(list as List<CarImageEntity>)
                }
                override fun onCancel() {

                }
            })
        }

    }
    //上传图片
    private fun uploadImages(list: List<CarImageEntity>){
        lifecycleScope.launch {
            mViewModel.channel.send(ParkSelectIntent.UploadCarImage(list))
        }
    }

    override suspend fun sendViewIntent() {
       mViewModel.channel.send(ParkSelectIntent.LoadCarColors)
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is ParkSelectUiState.LoadCarColorsSuccess->{
                    mColorAdapter = CarColorAdapter(R.layout.item_color)
                    mColorAdapter.data.addAll(it.data as MutableList<CarColorValues>)
                    mDataBinding.applyparkingRv.layoutManager=LinearLayoutManager(this@ParkSelectedActivity,LinearLayoutManager.HORIZONTAL,false)
                    mDataBinding.applyparkingRv.adapter=mColorAdapter
                    mColorAdapter.notifyDataSetChanged()
                }
                is ParkSelectUiState.UploadImageSuccess -> {
                    showToastMessage("上传成功")
                    Log.i("=======", "上传成功返回的图片地址: ${GsonUtils.toJson(it.pathUrls)}")
                }
                is ParkSelectUiState.LoadCarColorsFail->showToastMessage(it.msg)
                is ParkSelectUiState.LoadCarColorsFail -> showToastMessage(it.msg)
                else->{}
            }
        }
    }




}



